package com.leyou.user.web;

import com.leyou.user.dto.UserDTO;
import com.leyou.user.entity.User;
import com.leyou.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("/info")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 数据唯一性校验
     * @param data
     * @param type
     * @return
     */
    @GetMapping("/exists/{data}/{type}")
    public ResponseEntity<Boolean> checkExists(@PathVariable("data")String data,
                                               @PathVariable("type")Integer type) {

        return ResponseEntity.ok(this.userService.checkExists(data, type));
    }

    /**
     * 发送短信验证码
     * @param phone
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendMsg(@RequestParam("phone")String phone) {

        this.userService.sendMsg(phone);
        return ResponseEntity.ok().build();
    }

    /**
     *  用户注册
     *
     * @Valid 作用是  使实体类中的 服务端校验生效
     * @param user
     * @param code
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> UserRegister(
            @Valid User user, @RequestParam("code") String code
    ) {

        this.userService.UserRegister(user, code);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping
    public ResponseEntity<UserDTO> queryUserByNameAndPass(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        return ResponseEntity.ok(this.userService.queryUserByNameAndPass(username, password));
    }
}
